//
//  BaoXianViewController.m
//  wangzhe
//
//  Created by 李 佳伟 on 2017/11/3.
//  Copyright © 2017年 李 佳伟. All rights reserved.
//

#import "BaoXianViewController.h"
#import "JTCSM.h"
#import "BaoXianTableViewCell.h"
#import "BaoXianInfoViewController.h"
#import "BaoxianModel.h"
@interface BaoXianViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_tabelView;
    NSMutableArray *titlearray;
    NSString *page;
}
@end

@implementation BaoXianViewController
- (void)gotBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [df isRemoveTarbar:YES];
    self.navigationController.navigationBar.hidden = NO;
    self.view.backgroundColor = RGBACOLOR(241, 241, 241, 1);
    leftBarButtonItems_rightBarButtonItems *leftbtn = KCUSTOMNAVIGSTION_ALLOC;
    self.navigationItem.leftBarButtonItem = [leftbtn setBarButtonItemsImageName:@"fanhui.png" frame:KLeftBarButtonItemFrame_image target:self SEL:@selector(gotBack)];
    [self layoutTableView];
    [self theRequest];
    // Do any additional setup after loading the view.
}

- (void)layoutTableView{
    
    _tabelView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KCONTROLLERWith, KCONTROLLERHeight-50) style:UITableViewStylePlain];
    _tabelView.delegate = self;
    _tabelView.dataSource = self;
    [self.view addSubview:_tabelView];
    _tabelView.backgroundColor = RGBACOLOR(241, 241, 241, 1);
    _tabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self setUpTableView];
    //    view.backgroundColor = RGBACOLOR(241, 241, 241, 1);
}

- (void)setUpTableView{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [_tabelView addHeaderWithTarget:self action:@selector(headerRereshing)];
    
    // 上拉加载
    [_tabelView addFooterWithTarget:self action:@selector(footerRereshing)];
    
}
- (void)headerRereshing{
    // 2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(KRefreshTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //getMarriageSeeking(网络请求)
        [self theRequest];
    });
}
- (void)footerRereshing{
    // 2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(KRefreshTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //getMarriageSeeking(网络请求)
        [self loadMoreDataRequest];
        
    });
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return titlearray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
   NSString *cellid = [NSString stringWithFormat:@"textfield%ld",(long)indexPath.row];
   BaoXianTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
   if (cell == nil) {
       cell = [[BaoXianTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
   }
    if (indexPath == nil || indexPath.row >=titlearray.count) {
        return cell;
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    BaoxianModel *model = titlearray[indexPath.row];
    [cell setValueWithBaoxinModel:model];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [BaoXianTableViewCell whc_CellHeightForIndexPath:indexPath tableView:tableView];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BaoxianModel *model = titlearray[indexPath.row];
    BaoXianInfoViewController *vc = [[BaoXianInfoViewController alloc]init];
    vc.nameTitle = @"保险详情";
    vc.policy_id = model.policy_id;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)theRequest{
    [titlearray removeAllObjects];
    titlearray = nil;
    titlearray = [NSMutableArray array];
    
    LoadData *request = [[LoadData alloc]init];
    [request loadDataPostAsynchronousWithURL:KURLPOSTHeadr(@"UserPolicy/getList") withDict:@{@"token":KTokenString,@"p":@""} superView:nil isAddtag:NO Block:^(id result) {
        
        NSString *isreturn = [NSString stringWithFormat:@"%@",[result objectForKey:@"status"]];
        if ([isreturn isEqualToString:@"1"]) {
            
            NSDictionary *dict = KResultOfKey(@"data");
            NSArray *bannerArr = [dict objectForKey:@"list"];
            page = [dict objectForKey:@"p"];
            for (NSDictionary *bannerdict in bannerArr) {
                BaoxianModel *model = [BaoxianModel whc_ModelWithJson:bannerdict];
                [titlearray addObject:model];
            }
            
            [_tabelView reloadData];
            [_tabelView headerEndRefreshing];
        }
    }];
}

- (void)loadMoreDataRequest{
    
    LoadData *request = [[LoadData alloc]init];
    [request loadDataPostAsynchronousWithURL:KURLPOSTHeadr(@"Article/notice") withDict:@{@"token":KTokenString,@"p":page} superView:nil isAddtag:NO Block:^(id result) {
        
        NSString *isreturn = [NSString stringWithFormat:@"%@",[result objectForKey:@"status"]];
        if ([isreturn isEqualToString:@"1"]) {
            
            NSDictionary *dict = KResultOfKey(@"data");
            NSArray *bannerArr = [dict objectForKey:@"list"];
            page = [dict objectForKey:@"p"];
            for (NSDictionary *bannerdict in bannerArr) {
                BaoxianModel *model = [BaoxianModel whc_ModelWithJson:bannerdict];
                [titlearray addObject:model];
            }
            
            [_tabelView reloadData];
            [_tabelView footerEndRefreshing];
        }
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
